package com.hyt.it.ogt.pj.asynchronous;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.pj.enums.CommonEnums;
import com.hyt.it.ogt.pj.model.entity.PieceScore;
import com.hyt.it.ogt.pj.model.entity.PieceScoreLog;
import com.hyt.it.ogt.pj.service.IPieceScoreLogService;
import com.hyt.it.ogt.pj.service.IPieceScoreService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class BatchUpdatePieceScores implements Runnable {

    private IPieceScoreService pieceScoreService;
    private IPieceScoreLogService pieceScoreLogService;
	
	List<PieceScore> pieceScores = new ArrayList<>();
	
	public IPieceScoreService getPieceScoreService() {
		return pieceScoreService;
	}

	public void setPieceScoreService(IPieceScoreService pieceScoreService) {
		this.pieceScoreService = pieceScoreService;
	}


	public List<PieceScore> getPieceScores() {
		return pieceScores;
	}


	public void setPieceScores(List<PieceScore> pieceScores) {
		this.pieceScores = pieceScores;
	}
	
	


	public IPieceScoreLogService getPieceScoreLogService() {
		return pieceScoreLogService;
	}


	public void setPieceScoreLogService(IPieceScoreLogService pieceScoreLogService) {
		this.pieceScoreLogService = pieceScoreLogService;
	}


	@Override
	public void run() {
		try {
			log.info("BatchUpdatePieceScores start");
			List<PieceScoreLog> pieceScoreLogs = new ArrayList<>(pieceScores.size());
			for (PieceScore pieceScore : pieceScores) {
				pieceScore.setFinish(CommonEnums.PIECCE_SCORE_FINSH_1.getCode());
				BigDecimal subjectiveScore = new BigDecimal("0");
				BigDecimal objectiveScore = new BigDecimal("0");
				BigDecimal total = new BigDecimal("0");
				if (null != pieceScore.getSubjectiveScore()) {
					subjectiveScore = pieceScore.getSubjectiveScore();
				}
				if (null != pieceScore.getObjectiveScore()) {
					objectiveScore = pieceScore.getObjectiveScore();
				}
				total  = total.add(subjectiveScore).add(objectiveScore);
				pieceScore.setScore(total);
				pieceScore.setObjectiveScore(objectiveScore);
				pieceScore.setSubjectiveScore(subjectiveScore);
				pieceScore.setUpdateDate(LocalDateTime.now());
				pieceScore.setMarkEndTime(new Date());
				pieceScore.setLastMarkTime(new Date());
				Integer marktimes = pieceScore.getMarkTimes();
				if(null == marktimes) {
            		marktimes = Integer.valueOf(0);
            	}else {
            		marktimes = marktimes + 1;
            	}
				pieceScore.setMarkTimes(marktimes);
				pieceScoreLogs.add(buildPieceScoreLog(pieceScore));
			}
			pieceScoreService.saveOrUpdateBatch(pieceScores);
			pieceScoreLogService.saveOrUpdateBatch(pieceScoreLogs);
			log.info("BatchUpdatePieceScores start");
		} catch (Exception e) {
			log.error("BatchUpdatePieceScores error", e);
		}
	}
	
	private PieceScoreLog buildPieceScoreLog(PieceScore pieceScore) {
        PieceScoreLog pieceScoreLog = new PieceScoreLog();
        pieceScoreLog.setId(UUIDUtils.newSortUUID());
        pieceScoreLog.setObjectiveScore(pieceScore.getObjectiveScore());
        pieceScoreLog.setPieceScoreId(pieceScore.getId());
        pieceScoreLog.setScore(pieceScore.getScore());
        pieceScoreLog.setSubjectiveScore(pieceScore.getSubjectiveScore());
        pieceScoreLog.setUserId(pieceScore.getUserId());
        return pieceScoreLog;
    }
}
